package com.atech.ebayadmin.system.dao.impl;

import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import com.atech.ebayadmin.common.pager.Pager;
import com.atech.ebayadmin.common.util.BaseDAOHibernate;
import com.atech.ebayadmin.system.dao.IEbayActDAO;
import com.atech.ebayadmin.system.model.EbayAct;
import com.atech.ebayadmin.system.model.User;

public class EbayActDAO extends BaseDAOHibernate implements IEbayActDAO{
	@Override
	protected Class getObjClass() {
		return EbayAct.class;
	}
	public List<EbayAct> listEbayAct(String ebayAct, Pager pager) throws Exception{
		StringBuilder queryCountHql = new StringBuilder(this.queryCountHql(ebayAct));	
		StringBuilder queryListHql = new StringBuilder(this.queryListHql(ebayAct));
		Query queryCount = super.getSession().createQuery(queryCountHql.toString());
		Query queryList = super.getSession().createQuery(queryListHql.toString());
		int i =0;
		if(StringUtils.isNotBlank(ebayAct)){
			queryCount.setString(i, ebayAct);
			queryList.setString(i, ebayAct);
			i++;
		}
		Number total = (Number)queryCount.uniqueResult();
		pager.setTotalRows(total.intValue());
		queryList.setFirstResult(pager.getStartRow());
		queryList.setMaxResults(pager.getPageSize());
		return queryList.list();
	}
	public List<EbayAct> listEbayAct(String ebayAct) throws Exception{	
		StringBuilder queryListHql = new StringBuilder(this.queryListHql(ebayAct));
		Query queryList = super.getSession().createQuery(queryListHql.toString());
		int i =0;
		if(StringUtils.isNotBlank(ebayAct)){
			queryList.setString(i, ebayAct);
			i++;
		}
		return queryList.list();
	}
	private String queryCountHql(String ebayAct){
		StringBuilder sb = new StringBuilder();
		sb.append("select count(*) from ");
		sb.append(getObjClass().getName().toString());
		sb.append(this.createHqlString(ebayAct));
		return sb.toString();
	}
	private String queryListHql(String ebayAct){
		StringBuilder sb = new StringBuilder();
		sb.append("from ");
		sb.append(getObjClass().getName().toString());
		sb.append(this.createHqlString(ebayAct));
		return sb.toString();
	}
	private String createHqlString(String ebayAct){		
		StringBuilder sb = new StringBuilder();
		sb.append(" t where 1=1 ");
		if(StringUtils.isNotBlank(ebayAct)){
			sb.append(" and t.ebayAct = ? ");
		}
		sb.append(" and t.isDeleted = 0 order by t.id desc");
		return sb.toString();
	}
	public Integer saveEbayAct(EbayAct po) throws Exception{
		Object key = super.getHibernateTemplate().save(po);
		return (Integer) key;
	}
	public void updateEbayAct(EbayAct po) throws Exception{
		super.updateObject(po);
	}
	public EbayAct getEbayAct(Integer ebayActId) throws Exception{
		return (EbayAct)super.getHibernateTemplate().get(EbayAct.class, ebayActId);
	}
}
